package com.hawthorn.mq.service.impl;

import com.hawthorn.mq.service.LoginLogService;
import com.hawthorn.platform.utils.common.MyUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>todo 登录日志实现类</p>
 * @author: andy.ten@tom.com | xukaixing@hotmail.com
 * @created: 2022/7/10 11:23
 * @version v1.0.1
 * @copyright: Copyright (c)  andyten
 */
@Service
public class LoginLogServiceImpl implements LoginLogService
{
  @Autowired
  RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法

  @Override
  public void sendLoginLog(Map<String, Object> userInfo)
  {
    Map<String, Object> userData = (Map) userInfo.get("userInfo");

    String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    Map<String, Object> logData = new HashMap<>();
    logData.put("account", MyUtil.getValue(userData.get("account")));
    logData.put("msgData", userInfo);
    logData.put("createTime", createTime);
    rabbitTemplate.convertAndSend("hawthorn.topic.logLogin", "hawthorn.topic.logs.login", logData);
  }
}
